package com.tencent.ncnnyolox.adapter;

import android.graphics.drawable.AnimationDrawable;
import android.os.CountDownTimer;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.air4.chinesetts.tts.TtsManager;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.tencent.ncnnyolox.MyApplication;
import com.tencent.ncnnyolox.R;
import com.tencent.ncnnyolox.basemodule.utils.LiveEventKey;
import com.tencent.ncnnyolox.basemodule.utils.RegexUtils;
import com.tencent.ncnnyolox.discern.Message;

import java.util.List;

/**
 * <pre>
 *     author : yh
 *     e-mail : xxx@xx
 *     time   : 2023/12/14
 *     desc   :
 * </pre>
 */
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
    private List<Message> mMsgList;


    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivVoice;
        LinearLayout leftLayout;

        LinearLayout rightLayout;

        TextView leftMsg;

        TextView rightMsg;

        public ViewHolder(View view) {
            super(view);
            leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
            rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
            leftMsg = (TextView) view.findViewById(R.id.left_msg);
            rightMsg = (TextView) view.findViewById(R.id.right_msg);
            //声音播放
            ivVoice = (ImageView) view.findViewById(R.id.iv_voice);


        }
    }

    public MsgAdapter() {
        mMsgList  = MyApplication.application.getDaoSession().getMessageDao().loadAll();
    }

    public void addItem(Message message) {
        mMsgList.add(message);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Message msg = mMsgList.get(position);
        if (msg.getType() == Message.TYPE_RECEIVED) {
            //如果收到的消息，则显示左边的消息布局，将右边的消息布局隐藏
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);

        }else  {
            //如果是发出的消息，则显示右边的消息布局，将左边的消息布局隐藏
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg.setText(msg.getContent());
            holder.ivVoice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TtsManager.getInstance().stopTts();
                    ((AnimationDrawable) holder.ivVoice.getDrawable()).stop();
                    ((AnimationDrawable) holder.ivVoice.getDrawable()).start();
                    TtsManager.getInstance().speak(RegexUtils.removeEmojis(msg.getContent()),1.2f,false);
                    double time= Math.ceil( msg.getContent().length()/2);

                    new CountDownTimer((long) (time*1000L), 1000L){

                        @Override
                        public void onTick(long millisUntilFinished) {

                        }

                        @Override
                        public void onFinish() {
                            TtsManager.getInstance().stopTts();
                            ((AnimationDrawable) holder.ivVoice.getDrawable()).stop();
                            notifyDataSetChanged();
                        }
                    }.start();
                   // LiveEventBus.get(LiveEventKey.To_Speak).post(msg.getContent());
                    //  ((AnimationDrawable) holder.ivVoice.getDrawable()).stop();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }

}
